import router from './router'
import store from './store'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import { getToken } from './utils/utils'

NProgress.configure({ showSpinner: false })// NProgress Configuration

const whiteList = ['/login'] // 不重定向白名单
router.beforeEach((to, from, next) => {
  document.title = `${store.getters.projectName}-${to.meta.title}`
  NProgress.start()
  if (getToken()) {
    if (to.path === '/login') {
      next({ path: '/' })
      NProgress.done()
    } else {
      next()
      // if (!store.getters.login) {
      //   store.dispatch('GetInfo').then(res => { // 拉取用户信息
      //     next()
      //   }).catch((err) => {
      //     store.dispatch('FedLogOut').then(() => {
      //       Message.error(err || '登入过期, 请重新登录')
      //       next({ path: '/' })
      //     })
      //   })
      // } else {
      //   next()
      // }
    }
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      // 不在白名单则全部重定向到登录页
      next({ path: '/login', query: { redirect: to.fullPath } })
      NProgress.done()
    }
  }
})

router.afterEach(() => {
  NProgress.done() // 结束Progress
})
